package com.situ.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.springboot.mapper.BlogMapper;
import com.situ.springboot.pojo.Blog;
import com.situ.springboot.pojo.query.BlogQuery;
import com.situ.springboot.pojo.vo.BlogTypeCount;
import com.situ.springboot.service.IBlogService;
import com.situ.springboot.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogServiceImpl implements IBlogService {
    @Autowired
    private BlogMapper blogMapper;

    @Override
    public LayUITableResult list(BlogQuery blogQuery) {
        //开启分页
        PageHelper.startPage(blogQuery.getPage(), blogQuery.getLimit());
        //因为我们使用PageHelper开启了分页，那么查找所有的sql语句会自动拼接分页参数limit
        List<Blog> list = blogMapper.list(blogQuery);
        //查找一共多少数量，为了算一共有多少也
        PageInfo pageInfo = new PageInfo(list);
        long totalCount = pageInfo.getTotal();

        return LayUITableResult.ok((int) totalCount, list);
    }

    @Override
    public void deleteById(Integer id) {
        blogMapper.deleteById(id);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            blogMapper.deleteById(id);
        }
    }

    @Override
    public void add(Blog blog) {
        blogMapper.add(blog);
    }

    @Override
    public List<BlogTypeCount> selectBlogTypeCount() {
        return blogMapper.selectBlogTypeCount();
    }

}
